Ext.define('TS.controller.Customer', {
    extend: 'Ext.app.Controller',
    models: ['base.CustomerModel','base.CustomerStateModel'],
    stores: [
        'base.CustomerStore'
    ],
    views: [
        'base.CustomerList','base.CustomerSimpleList','base.CustomerSearchDialog',
        'base.CustomerEditer'
    ],
    init: function() {
        this.control({ 
        	'custlist':{itemdblclick: this.itemEditCust },
        	'custlist button[action=upd]': { click: this.btnUpdateCust },
        	'custlist button[action=add]': { click: this.addCust },
        	'custlist button[action=del]': { click: this.delCust},
        	'custlist button[action=ref]': { click: this.refCust},
        	'custlist button[action=recycle]': { click: this.recycleCust},
        	'custlist button[action=restore]': { click: this.restoreCust},
        	'custlist button[action=exportxls]': { click: this.exportxls},
        	'custlist triggerfield': { specialkey: this.serchCust},
	        'customerediter button[action=save]':{click:this.editCust}
        });
    },
    exportxls:function(btn){
    	window.open("base/getCustomerXLS.do");
    },
    addCust:function(btn){
    	var grid=btn.ownerCt.ownerCt;
    	var record=Ext.create('TS.model.base.CustomerModel');
    	record.set('parentId',0);
    	record.set('acctEnabled',true);
    	record.set('creditAmount',10000);
    	record.set('creditDay',5);
    	this.itemEditCust(grid,record);
    },
    itemEditCust:function(grid,record){
    	var view = Ext.widget('customerediter');
    	view.down('form').loadRecord(record);
    	view.show();
    },
    editCust:function(btn){
     	var win    = btn.up('window'),
        form   = win.down('form');
        //非空检查
        if(!form.isValid())
        {
        	return;
        }
        var record = form.getRecord(),
        values = form.getValues();
        record.set(values);
               
	    win.close();
	    var panel=Ext.ComponentQuery.query('custlist');
	    var data=record.getData(false);
	    
	    Ext.Ajax.request({
				    url:'base/updcustomer.do',
				    method : 'POST',
				    timeout :3000,
				    jsonData:data,
				    success:function(response){		
				    	var data=Ext.decode(response.responseText);
				    	if(data.success==true){
				    		panel[0].store.reload();
				    	}
				    	showExtReturnMsg(response);
				    }
	    });   	
    },
    btnUpdateCust:function(btn){
    	var grid=btn.ownerCt.ownerCt;
    	var data=grid.getSelectionModel().getSelection();
    	if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			var record=data[0];
			var view = Ext.widget('customerediter');
        	view.down('form').loadRecord(record);
        	view.show();
		}
    },
    serchCust:function(field,e){
    	if(e.keyCode==e.ENTER){
	    	var view=Ext.create('TS.view.base.CustomerSearchDialog');
	    	view.setVal(field.value);
	    	view.on({
			    selected: function(records){
			    	var panel=field.ownerCt.ownerCt;
			    	panel.store.loadRecords(records);
			    	//panel.store.commitChanges();
			    	//panel.store.sync();
			    	//panel.down('pagingtoolbar').bindStore(panel.store);
			    	//panel.down('pagingtoolbar').refreshInfo(records.length);
			    	//panel.down('pagingtoolbar').updateInfo();
			    	//panel.store.reload();
			    },
			    scope: this
			});
	    	view.show();
    	}
    },
    delCust:function(btn){
    	var grid = btn.ownerCt.ownerCt;
    	//获取选择的行集合.即record数组
		var data = grid.getSelectionModel().getSelection();
		if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			Ext.Msg.confirm('删除','真的要删除所选择的客户吗?',function(b){
				if(b=='yes'){
					//1.先得到ID的数据(customerId)
					var st = grid.getStore();
					var ids = [];
					Ext.Array.each(data,function(record){
						ids.push(record.get('customerId'));
					});
					//2.后台操作(delemp)
					Ext.Ajax.request({
						url:'base/delCust.do',
						params:{ids:ids.join(",")},
						method:'POST',
						timeout:3000,
						success:function(response,opts){
							//3.前端操作DOM进行删除(ExtJs)
							var rs=Ext.decode(response.responseText);
					    	if(rs.success==true){
					    		Ext.Array.each(data,function(record){
									st.remove(record);
								});
					    	}
					    	showExtReturnMsg(response);
							
						}
					});
				}
			});
		}
    },
    recycleCust:function(btn){
    	var grid=btn.ownerCt.ownerCt;
    	grid.store.load({
    		url:'base/recycleCust.do',
    		callback:function(records, operation, success){
    			if(success==false)
    				Ext.Msg.show({title:'ThinkGSP',msg:'查询回收站数据出错!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
    		},
	   		scope: this
    	})
    },
    restoreCust:function(btn){
    	var grid=btn.ownerCt.ownerCt;
    	var data=grid.getSelectionModel().getSelection();
    	if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			var st = grid.getStore();
			Ext.Ajax.request({
				url:'base/restoreCust.do',
				params:{custId:data[0].get("customerId")},
				method:'POST',
				timeout:3000,
				success:function(response,opts){
					//3.前端操作DOM进行删除(ExtJs)
					var rs=Ext.decode(response.responseText);
			    	if(rs.success==true){
			    		Ext.Array.each(data,function(record){
			    			if(record.get('deleted'))
								st.remove(record);
						});
			    	}
			    	showExtReturnMsg(response);
					
				}
			});
		}
    },
    refCust:function(btn){
    	var grid=btn.ownerCt.ownerCt;
    	grid.store.reload();
    }
});